package bigapps.myneighbor.client;

import bigapps.myneighbor.shared.FieldVerifier;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.FileUpload;
import com.google.gwt.user.client.ui.FormPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.RadioButton;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class FileUploadEntryPoint implements EntryPoint {
	private Panel panelRadio;
	private Panel panelBrowseFile;
	private Panel panelSubmit;
	private Label lb_radio;
	private RadioButton radio_1;
	private RadioButton radio_2;
      private FileUpload upload;
      private Button buttonSubmit;
      private ListBox dropBox;
      private Button buttonSubmit2;
  	private HTML labelResult;
  	private Label labelAction;
      
      private enum REQUEST_OPTION {
    	  LOAD_FROM_STAGING("Load from staging"),
    	  UPDATE_LATITUDE_LONGITUDE("Update latitude and longitude"),
    	  FIX_LATITUDE_LONGITUDE("Fix latitude and longitude"),
    	  STATUS("Get status");
    	  
    	  private String display;
    	  
    	  private REQUEST_OPTION(String display){
    		  this.display = display;
    	  }
    	  
    	  public String toString(){
    		  return display;
    	  }
      }
	/**
	 * The message displayed to the user when the server cannot be reached or
	 * returns an error.
	 */
	private static final String SERVER_ERROR = "An error occurred while "
			+ "attempting to contact the server. Please check your network "
			+ "connection and try again.";

	/**
	 * Create a remote service proxy to talk to the server-side Greeting service.
	 */

	/**
	 * This is the entry point method.
	 */
	public void onModuleLoad() {
	    final FormPanel form = new FormPanel();   
	    final FormPanel form2 = new FormPanel();
		final Label errorLabel = new Label();

		this.panelRadio = new VerticalPanel();
		this.panelRadio.setTitle("Data Type");
		this.lb_radio = new Label("type");
		this.radio_1 = new RadioButton("group", "municipal parking");
		this.radio_1.setEnabled(false);
		this.radio_2 = new RadioButton("group", "parking");
		this.panelRadio.add(this.lb_radio);
		this.panelRadio.add(this.radio_1);
		this.panelRadio.add(this.radio_2);
		this.labelAction = new Label("Choose action from followings:");
		RootPanel.get("panelRadio").add(this.panelRadio);
		ValueChangeHandler<Boolean> radioValueChangeHandler = new ValueChangeHandler<Boolean>() {
			@Override
			public void onValueChange(ValueChangeEvent<Boolean> event) {
				System.out.println(((RadioButton)event.getSource()).getText());
				if("municipal parking".equals(((RadioButton)event.getSource()).getText())){
System.out.println("municipal_parking");
					form.setAction("/controller/municipal_parking/load_file");   
				}
				else if("parking".equals(((RadioButton)event.getSource()).getText())){
System.out.println("parking");
					form.setAction("/controller/parking_facility/load_file");
				}
			}
		};
		this.radio_1.addValueChangeHandler(radioValueChangeHandler);
		this.radio_2.addValueChangeHandler(radioValueChangeHandler);
		
		form.setAction("/controller/parking_facility/load_file");   
	    
	    form.setEncoding(FormPanel.ENCODING_MULTIPART);   
	    form.setMethod(FormPanel.METHOD_POST);   
	       
	    panelBrowseFile= new VerticalPanel();
          panelSubmit = new VerticalPanel();   
	    form.setWidget(panelBrowseFile); 
	    form2.setWidget(panelSubmit);
	  
	    upload = new FileUpload();   
	    upload.setName("uploadFormElement");   
	    panelBrowseFile.add(upload);   

          buttonSubmit = new Button("Submit", new ClickHandler() {   
	      public void onClick(ClickEvent event) {  
System.out.println("submit clicked"); 
	        form.submit();   
	      }  
          });
	    // Add a 'submit' button.   
	    panelBrowseFile.add(buttonSubmit);
		
	    labelResult = new HTML();
	    dropBox = new ListBox(false);
	    dropBox.addItem(REQUEST_OPTION.LOAD_FROM_STAGING.toString());
	    dropBox.addItem(REQUEST_OPTION.UPDATE_LATITUDE_LONGITUDE.toString());
	    dropBox.addItem(REQUEST_OPTION.FIX_LATITUDE_LONGITUDE.toString());
	    dropBox.addItem(REQUEST_OPTION.STATUS.toString());
	     buttonSubmit2 = new Button("Submit", new ClickHandler(){
	    	 @Override
	    	public void onClick(ClickEvent event)
	    	{
	    		System.out.println(dropBox.getName());
			    form2.setMethod(FormPanel.METHOD_GET);   
	    		if(REQUEST_OPTION.LOAD_FROM_STAGING.toString().equals(dropBox.getItemText(dropBox.getSelectedIndex()))){
					form2.setAction("/queue/parkingFacilityUpdateDataQueue");   
	    		}
	    		else if(REQUEST_OPTION.UPDATE_LATITUDE_LONGITUDE.toString().equals(dropBox.getItemText(dropBox.getSelectedIndex()))){
					form2.setAction("/queue/parkingFacilityQueue");   
	    		}
	    		else if(REQUEST_OPTION.FIX_LATITUDE_LONGITUDE.toString().equals(dropBox.getItemText(dropBox.getSelectedIndex()))){
					form2.setAction("/queue/parkingFacilityRepairQueue");   
	    		}
	    		else if(REQUEST_OPTION.STATUS.toString().equals(dropBox.getItemText(dropBox.getSelectedIndex()))){
					form2.setAction("/controller/parking_facility/get_status");   
	    		}
	    		form2.submit();
	    	}
	    });
	     
	     panelSubmit.add(dropBox);
	     panelSubmit.add(buttonSubmit2);
	     panelSubmit.add(labelResult);
	     
	     form.addSubmitCompleteHandler(new FormPanel.SubmitCompleteHandler(){
	    	 public void onSubmitComplete(FormPanel.SubmitCompleteEvent event){
	    		  labelResult.setHTML("<B>Please wait...</B>");
	    		  labelResult.setHTML("<B>" + event.getResults() + "</B>");
	    	 }
	     });
	     form2.addSubmitCompleteHandler(new FormPanel.SubmitCompleteHandler(){
	    	 public void onSubmitComplete(FormPanel.SubmitCompleteEvent event){
	    		  labelResult.setHTML("<B>Please wait...</B>");
	    		  labelResult.setHTML("<B>" + event.getResults() + "</B>");
	    	 }
	     });
		// Add the nameField and sendButton to the RootPanel
		// Use RootPanel.get() to get the entire body element
		RootPanel.get("browseFileContainer").add(form);
		RootPanel.get("submitButtonContainer").add(form2);
	}
}
